package com.bestcem.xm.member.controller;

import cn.hutool.core.lang.ObjectId;
import com.bestcem.xm.common.core.domain.web.ServiceResult;
import com.bestcem.xm.common.core.domain.web.XmResultJson;
import com.bestcem.xm.common.core.enums.ReqErrorCodeEnum;
import com.bestcem.xm.component.security.context.SecurityContextHolder;
import com.bestcem.xm.component.security.dto.TokenDTO;
import com.bestcem.xm.member.controller.param.group.CreateGroupParam;
import com.bestcem.xm.member.controller.param.group.UpdateGroupParam;
import com.bestcem.xm.member.controller.vo.CreateGroupVO;
import com.bestcem.xm.member.controller.vo.DeleteGroupVO;
import com.bestcem.xm.member.controller.vo.ListGroupVO;
import com.bestcem.xm.member.controller.vo.UpdateGroupVO;
import com.bestcem.xm.member.service.GroupService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Objects;

/**
 * 分组Controller
 *
 * @author ympan <yuming.pan@idiaoyan.com>
 * @version v1.0
 * @date 22:58 2021/12/14
 */
@Slf4j
@RestController
@RequestMapping("/member/group")
public class GroupController {

    @Resource
    private GroupService groupService;

    /**
     * 创建分组
     *
     * @param param 创建分组入参
     * @author ympan <yuming.pan@idiaoyan.com>
     * @date 16:14 2021/11/24
     */
    @PostMapping
    public XmResultJson<CreateGroupVO> createGroup(@RequestBody @Valid CreateGroupParam param,
                                                   HttpServletRequest request) {
        // 获取企业id
        
        String orgId = SecurityContextHolder.getOrgId();
        if (StringUtils.isBlank(orgId)) {
            log.error("【联系人】企业标识格式错误，orgId:{}", orgId);
            return XmResultJson.fail(ReqErrorCodeEnum.PARAM_FORMAT.getCode(), "创建分组失败");
        }

        ServiceResult<CreateGroupVO> result = groupService.createGroup(param, orgId);
        return XmResultJson.handleServiceResult(result);
    }

    /**
     * 更新分组
     *
     * @param id    分组id
     * @param param 更新分组参数
     * @author ympan <yuming.pan@idiaoyan.com>
     * @date 11:52 2021/11/25
     */
    @PutMapping("/{id}")
    public XmResultJson<UpdateGroupVO> updateGroup(@PathVariable("id") String id,
                                                       @RequestBody @Valid UpdateGroupParam param,
                                                       HttpServletRequest request) {
        // 获取企业id
        
        String orgId = SecurityContextHolder.getOrgId();
        if (StringUtils.isBlank(orgId)) {
            log.error("【联系人】企业标识格式错误，orgId:{}", orgId);
            return XmResultJson.fail(ReqErrorCodeEnum.PARAM_FORMAT.getCode(), "更新分组失败");
        }

        // 校验入参
        if (StringUtils.isBlank(id)) {
            return XmResultJson.fail(ReqErrorCodeEnum.PARAM_LOST.getCode(), "id不能为空");
        }
        if (!ObjectId.isValid(id)) {
            return XmResultJson.fail(ReqErrorCodeEnum.PARAM_FORMAT.getCode(), "分组标识格式错误");
        }
        if (Objects.isNull(param.getAuto()) && Objects.isNull(param.getCondition()) && StringUtils.isBlank(param.getName())) {
            return XmResultJson.success();
        }

        ServiceResult<UpdateGroupVO> result = groupService.updateGroup(id, param, orgId);
        return XmResultJson.handleServiceResult(result);
    }

    /**
     * 查询分组列表
     *
     * @author ympan <yuming.pan@idiaoyan.com>
     * @date 11:52 2021/11/25
     */
    @GetMapping
    public XmResultJson<ListGroupVO> listGroup(HttpServletRequest request) {
        // 获取企业id
        
        String orgId = SecurityContextHolder.getOrgId();
        
         String userId = SecurityContextHolder.getUserId();
        // token获取用户id
        
         TokenDTO tokenDTO = SecurityContextHolder.getToken();
        if (StringUtils.isBlank(orgId)) {
            log.error("【联系人】企业标识格式错误，orgId:{}", orgId);
            return XmResultJson.fail(ReqErrorCodeEnum.PARAM_FORMAT.getCode(), "查询分组失败");
        }

        ServiceResult<ListGroupVO> result = groupService.listGroup(orgId, userId, tokenDTO.getSuperRole(), tokenDTO.getIsSenior());
        return XmResultJson.handleServiceResult(result);
    }

    /**
     * 删除分组
     *
     * @param id 分组id
     * @author ympan <yuming.pan@idiaoyan.com>
     * @date 11:52 2021/11/25
     */
    @DeleteMapping("/{id}")
    public XmResultJson<DeleteGroupVO> deleteGroup(@PathVariable("id") String id,
                                                       HttpServletRequest request) {
        // 获取企业id
        
        String orgId = SecurityContextHolder.getOrgId();
        if (StringUtils.isBlank(orgId)) {
            log.error("【联系人】企业标识格式错误，orgId:{}", orgId);
            return XmResultJson.fail(ReqErrorCodeEnum.PARAM_FORMAT.getCode(), "删除分组失败");
        }

        // 校验入参
        if (StringUtils.isBlank(id)) {
            return XmResultJson.fail(ReqErrorCodeEnum.PARAM_LOST.getCode(), "id不能为空");
        }
        if (!ObjectId.isValid(id)) {
            return XmResultJson.fail(ReqErrorCodeEnum.PARAM_FORMAT.getCode(), "分组标识格式错误");
        }

        ServiceResult<DeleteGroupVO> result = groupService.deleteGroup(id, orgId);
        return XmResultJson.handleServiceResult(result);
    }
}
    